<template>
  <div class="userForm">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm loginForm goods-form"
    >
      <el-form-item label="头像">
        <label for="goodsImg" class="goods-img"
          ><i class="el-icon-plus"></i><img :src="imageUrl" alt=""
        /></label>
        <input type="file" id="goodsImg" name="goodsImg" @change="imgChange" />
        <!-- <i v-else class="el-icon-plus avatar-uploader-icon"></i> -->
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="ruleForm.nickName"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="phone">
        <el-input
          type="password"
          show-password
          v-model="ruleForm.passWord"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="phone">
        <el-radio v-model="ruleForm.state" :label="true">正常</el-radio>
        <el-radio v-model="ruleForm.state" :label="false">禁用</el-radio>
      </el-form-item>
      <el-form-item
        label="权限"
        prop="phone"
        v-if="$store.state.user.role == 'superstar'"
      >
        <el-radio-group v-model="ruleForm.role">
          <el-radio label="user">用户</el-radio>
          <el-radio label="admin">管理员</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item prop="email" label="邮箱">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="ruleForm.gender">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
          <el-radio label="2">保密</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "UserForm",
  data() {
    return {
      imageUrl: process.env.VUE_APP_BASE_API + this.$store.state.user.avatar,
      ruleForm: {
        gender: "",
        email: "",
        phone: "",
        nickName: "",
        file: {},
        state: "",
        role: "",
      },
      rules: {
        nickName: [
          {
            min: 1,
            max: 18,
            message: "长度在 1 到 18 个字符",
            trigger: "blur",
          },
          {
            pattern: /^([\u4e00-\u9fa5]|\d|\w){1,12}$/,
            message: "请输入1-12个汉字、字母、数字和“_”组合",
            trigger: ["blur"],
          },
        ],
        email: [
          { required: false, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur"],
          },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^\d{11}$/,
            message: "手机号格式错误",
            trigger: ["change", "blur"],
          },
        ],
      },
    };
  },
  created() {
    // console.log(this.$attrs, "attr");
    let data = this.$attrs.data;
    // console.log(data);
    this.imageUrl = process.env.VUE_APP_BASE_API + data.avatar;
    this.ruleForm = this.$attrs.data;
  },
  mounted() {},
  methods: {
    imgChange(e) {
      //   console.dir(e.target.files[0]);
      //   this.ruleForm.file = e.target.files[0];
      this.imageUrl = URL.createObjectURL(e.target.files[0]);
      this.$emit("imgChange", e.target.files[0]);
    },
  },
};
</script>
<style scoped>
.goods-form img {
  display: inline-block;
  height: 100%;
  position: absolute;
  left: 180px;
}
.goods-img {
  display: inline-block;
  width: 178px;
  height: 178px;
  line-height: 178px;
  font-size: 80px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  position: relative;
  cursor: pointer;
}
#goodsImg {
  display: none;
}
</style>